package find;

/**
 * @author justnow
 * Created on 2020-09-24
 * Description
 * 二分查找
 */
public class BinaryFind {
    public int search(int[] nums, int target) {
        int length = nums.length;
        int low = 0, high = length - 1;
        while (low <= high) { //如果该该元素只有一个元素的情况
            int middle = (low + high) / 2;
            if (nums[middle] > target) {
                high--;
            } else if (nums[middle] < target) {
                low++;
            } else {
                return middle;
            }
        }
        return -1;
    }
}
